"""
使用requests
https://tieba.baidu.com/f?kw=浙江工贸&pn=0
https://tieba.baidu.com/f?kw=浙江工贸&pn=50

"""
import requests


def load_page(url, file_name):
    h = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
    }
    print('正在保存' + file_name)
    html = requests.get(url, headers=h)
    with open(file_name, 'w', encoding='utf-8')as f:
        f.write(html.content.decode())


kw = input("贴吧名字")
begin = int(input("起始页"))
end = int(input("请输入结束页"))

for i in range(begin, end + 1):
    url = "https://tieba.baidu.com/f?kw=" + kw + "&pn=" + str(i * 50 - 50)
    file_name = kw + '第' + str(i) + '页.html'
    load_page(url, file_name)
